﻿#include "SnakeTotem.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"
#include "../../Settings.h"
#include "CharmedSnake.h"
#include "../../../Shared/ServerPackets.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    SnakeTotem::SnakeTotem(MonsterInfo *info) : MonsterObject(info)
    {
        ActionTime = getEnvir()->getTime() + 1000;
        setDirection(MirDirection::Up);
    }

    std::string SnakeTotem::getName() const
    {
        return Master == nullptr ? Info->getGameName() : (Dead ? Info->getGameName() : StringHelper::formatSimple("{0}({1})", Info->getGameName(), Master->getName()));
    }

    void SnakeTotem::setName(const std::string &value)
    {
        throw NotSupportedException();
    }

    void SnakeTotem::Turn(MirDirection dir)
    {
    }

    bool SnakeTotem::Walk(MirDirection dir)
    {
        return false;
    }

    void SnakeTotem::ProcessRoam()
    {
    }

    void SnakeTotem::Process()
    {
        MaxMinions = PetLevel + 1;
        if (!Dead && Summoned)
        {
            bool selfDestruct = false;
            if (Master != nullptr)
            {
                if (Master->getCurrentMap() != getCurrentMap() || !Functions::InRange(Master->getCurrentLocation(), getCurrentLocation(), 15))
                {
                    selfDestruct = true;
                }
                if (Summoned && getEnvir()->getTime() > AliveTime)
                {
                    selfDestruct = true;
                }
                if (selfDestruct)
                {
                    Die();
                    DieTime = getEnvir()->getTime() + 3000;
                }
            }
            MonsterObject::Process();
        }
        else if (getEnvir()->getTime() >= DieTime)
        {
            Despawn();
        }
    }

    void SnakeTotem::Process(DelayedAction *action)
    {
        switch (action->Type)
        {
            case DelayedType::Damage:
                CompleteAttack(action->Params);
                break;
        }
    }

    void SnakeTotem::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        //Search for target
        if (getEnvir()->getTime() < SearchTime)
        {
            return;
        }
        SearchTime = getEnvir()->getTime() + SearchDelay;

        //Cant agro when shocked
        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }
        //update targets all the time ?
        AgroAllMobsInRange();

        //Refresh Minions
        for (int i = SlaveList.size() - 1; i >= 0; i--)
        {
            if (SlaveList[i]->Dead || SlaveList[i]->Node == nullptr)
            {
                SlaveList[i]->DeadTime = 0;
            }
        }

        //Keep Minions Updated
        if (SlaveList.size() < MaxMinions)
        {
            SpawnMinion();
        }
    }

    void SnakeTotem::AgroAllMobsInRange()
    {
        for (int d = 0; d <= Info->ViewRange; d++)
        {
            for (int y = getCurrentLocation()->Y - d; y <= getCurrentLocation()->Y + d; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = getCurrentLocation()->X - d; x <= getCurrentLocation()->X + d; x += std::abs(y - getCurrentLocation()->Y) == d ? 1 : d * 2)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }

                    Cell *cell = getCurrentMap()->GetCell(x, y);
                    if (!cell->getValid() || cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int i = 0; i < cell->Objects.size(); i++)
                    {
                        MapObject *ob = cell->Objects[i];
                        switch (ob->getRace())
                        {
                            case ObjectType::Monster:
                                if (!ob->IsAttackTarget(this))
                                {
                                    continue;
                                }
                                if (ob->getHidden() && (!CoolEye || getLevel() < ob->getLevel()))
                                {
                                    continue;
                                }
                                if ((static_cast<MonsterObject*>(ob))->Info->CoolEye == 100)
                                {
                                    continue;
                                }
                                ob->setTarget(this); //Agro the mobs in range - Very simple agro system overwriting mobs target
                                continue;
                            default:
                                continue;
                        }
                    }
                }
            }
        }
    }

    bool SnakeTotem::SpawnMinion()
    {
        if (Pets.size() >= MaxMinions)
        {
            return false;
        }

        MonsterInfo *info = getEnvir()->GetMonsterInfo(Settings::SnakesName);
        if (info == nullptr)
        {
            return false;
        }

        MonsterObject *monster;
        monster = MonsterObject::GetMonster(info);
        monster->PetLevel = PetLevel;
        monster->Master = this;
        monster->MaxPetLevel = static_cast<unsigned char>(1 + PetLevel * 2);
        monster->setDirection(getDirection());
        monster->ActionTime = getEnvir()->getTime() + 1000;

        (static_cast<Monsters::CharmedSnake*>(monster))->AliveTime = getEnvir()->getTime() + ((PetLevel * 2000) + 10000);
        (static_cast<Monsters::CharmedSnake*>(monster))->MasterTotem = this;

        SlaveList.push_back(monster);
        monster->Spawn(getCurrentMap(), getCurrentLocation());

        return true;
    }

    void SnakeTotem::Die()
    {
        MonsterObject::Die();

        DeadTime = 0;

        //Kill Minions
        for (int i = SlaveList.size() - 1; i >= 0; i--)
        {
            if (!SlaveList[i]->Dead && SlaveList[i]->Node != nullptr)
            {
                SlaveList[i]->Die();
                SlaveList[i]->DeadTime = 0;
            }
        }
    }

    void SnakeTotem::Spawned()
    {
        MonsterObject::Spawned();
        Summoned = true;
    }

    Packet *SnakeTotem::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Monster::SnakeTotem;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->Extra = Summoned;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
